package com.vaadin.thomas.contenttool.ui;

import java.util.Date;

import org.vaadin.hene.expandingtextarea.ExpandingTextArea;

import com.vaadin.thomas.contenttool.data.Localization;
import com.vaadin.thomas.contenttool.data.Value;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window;

class AddLocalizationWithStartWindow extends Window {

	private static final long serialVersionUID = 3539069012156340064L;

	public AddLocalizationWithStartWindow(final Value value,
			final Localization loc, final ExpandableValueComponent listener) {
		center();
		setWidth(40, UNITS_PERCENTAGE);
		setModal(true);
		((AbstractOrderedLayout) getContent()).setSpacing(true);
		getContent().setHeight(null);
		setHeight(null);
		setIcon(Util.getResource("icons/clock_add"));

		setCaption(AdminMainLayout.ct().get("contenttool.content.copy.msg"));

		final ExpandingTextArea valueField = new ExpandingTextArea();
		valueField.setValue(loc.getValue());
		valueField.setWidth(99, UNITS_PERCENTAGE);
		addComponent(valueField);

		final DateField startDate = new DateField();
		// startDate.setCaption(MainLayout.ct().get(
		// "contenttool.content.copy.start"));
		startDate.setLocale(AdminMainLayout.ct().getLang());
		startDate.setResolution(DateField.RESOLUTION_MIN);
		startDate.setValue(loc.getStartDate() == null ? new Date() : loc
				.getStartDate());

		Button add = new Button();
		add.setCaption(AdminMainLayout.ct().get("contenttool.content.copy.add"));
		add.addListener(new ClickListener() {

			private static final long serialVersionUID = 1314236768954523983L;

			@Override
			public void buttonClick(ClickEvent event) {
				listener.getPossiblyNewValueForTags().addLocalization(
						AdminMainLayout
								.ct()
								.getModel()
								.createLocalization(loc.getLanguage(),
										(String) valueField.getValue(),
										(Date) startDate.getValue()));
				listener.refreshHeader();
				listener.refreshContent();
				close();
			}
		});

		HorizontalLayout hl = new HorizontalLayout();
		hl.setWidth(100, UNITS_PERCENTAGE);
		hl.setSpacing(true);
		hl.addComponent(startDate);
		hl.addComponent(add);
		hl.setComponentAlignment(startDate, Alignment.BOTTOM_RIGHT);
		hl.setComponentAlignment(add, Alignment.BOTTOM_LEFT);
		addComponent(hl);
	}
}